/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mobsandgeeks.adapters.samples;

import com.mobsandgeeks.adapters.samples.fraction.CircularListFraction;
import com.mobsandgeeks.adapters.samples.fraction.SectionedListFraction;
import com.mobsandgeeks.adapters.samples.fraction.SimpleListFraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;

import java.util.List;

/**
 * pager适配器
 *
 * @since 2021-06-30
 */
public class DynamicFragmentPagerAdapter extends PageSliderProvider {
    private static final int NUM_2 = 2;
    private String[] mStrings = new String[]{"SIMPLELISTFRAGMENT", "SECTIONEDLISTFRAGMENT", "CIRCULARLISTFRAGMENT"};
    private List<Component> fragments;
    private FractionAbility fractionAbility;

    /**
     * 构造
     *
     * @param fragments
     * @param ability
     */
    public DynamicFragmentPagerAdapter(List<Component> fragments, FractionAbility ability) {
        this.fragments = fragments;
        this.fractionAbility = (FractionAbility) ability;
    }

    @Override
    public int getCount() {
        return fragments == null ? 0 : fragments.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        componentContainer.addComponent(fragments.get(i));
        if (i == 0) {
            fractionAbility.getFractionManager().startFractionScheduler()
                    .add(ResourceTable.Id_partentt, new SimpleListFraction()).submit();
        } else if (i == 1) {
            fractionAbility.getFractionManager().startFractionScheduler()
                    .add(ResourceTable.Id_partentt2, new SectionedListFraction()).submit();
        } else if (i == NUM_2) {
            fractionAbility.getFractionManager().startFractionScheduler()
                    .add(ResourceTable.Id_partentt3, new CircularListFraction()).submit();
        }
        return fragments.get(i);
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent(fragments.get(i));
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return false;
    }

    @Override
    public String getPageTitle(int position) {
        if (mStrings != null && position < mStrings.length) {
            return mStrings[position];
        }
        return "SIMPLELISTFRAGMENT";
    }
}
